package edu.hcmiu.portal.service;

import com.liferay.portal.kernel.log.Log;
import com.liferay.portal.kernel.log.LogFactoryUtil;
import com.liferay.portal.model.BaseModel;

import edu.hcmiu.portal.model.hook_activityClp;
import edu.hcmiu.portal.model.hook_categoryClp;
import edu.hcmiu.portal.model.hook_taglistClp;
import edu.hcmiu.portal.model.hook_userClp;

import java.lang.reflect.Method;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;


public class ClpSerializer {
    public static final String SERVLET_CONTEXT_NAME = "liferay-activities-hook";
    private static Log _log = LogFactoryUtil.getLog(ClpSerializer.class);
    private static ClassLoader _classLoader;

    public static void setClassLoader(ClassLoader classLoader) {
        _classLoader = classLoader;
    }

    public static Object translateInput(BaseModel oldModel) {
        Class<?> oldModelClass = oldModel.getClass();

        String oldModelClassName = oldModelClass.getName();

        if (oldModelClassName.equals(hook_activityClp.class.getName())) {
            hook_activityClp oldCplModel = (hook_activityClp) oldModel;

            ClassLoader contextClassLoader = Thread.currentThread()
                                                   .getContextClassLoader();

            try {
                Thread.currentThread().setContextClassLoader(_classLoader);

                try {
                    Class<?> newModelClass = Class.forName("edu.hcmiu.portal.model.impl.hook_activityImpl",
                            true, _classLoader);

                    Object newModel = newModelClass.newInstance();

                    Method method0 = newModelClass.getMethod("setActID",
                            new Class[] { Long.TYPE });

                    Long value0 = new Long(oldCplModel.getActID());

                    method0.invoke(newModel, value0);

                    Method method1 = newModelClass.getMethod("setActName",
                            new Class[] { String.class });

                    String value1 = oldCplModel.getActName();

                    method1.invoke(newModel, value1);

                    Method method2 = newModelClass.getMethod("setUserID",
                            new Class[] { Long.TYPE });

                    Long value2 = new Long(oldCplModel.getUserID());

                    method2.invoke(newModel, value2);

                    Method method3 = newModelClass.getMethod("setCatID",
                            new Class[] { Long.TYPE });

                    Long value3 = new Long(oldCplModel.getCatID());

                    method3.invoke(newModel, value3);

                    Method method4 = newModelClass.getMethod("setTaglistID",
                            new Class[] { Long.TYPE });

                    Long value4 = new Long(oldCplModel.getTaglistID());

                    method4.invoke(newModel, value4);

                    Method method5 = newModelClass.getMethod("setActTime",
                            new Class[] { Date.class });

                    Date value5 = oldCplModel.getActTime();

                    method5.invoke(newModel, value5);

                    Method method6 = newModelClass.getMethod("setActDuration",
                            new Class[] { Integer.TYPE });

                    Integer value6 = new Integer(oldCplModel.getActDuration());

                    method6.invoke(newModel, value6);

                    Method method7 = newModelClass.getMethod("setActSubject",
                            new Class[] { String.class });

                    String value7 = oldCplModel.getActSubject();

                    method7.invoke(newModel, value7);

                    Method method8 = newModelClass.getMethod("setActModel",
                            new Class[] { String.class });

                    String value8 = oldCplModel.getActModel();

                    method8.invoke(newModel, value8);

                    Method method9 = newModelClass.getMethod("setActFrequency",
                            new Class[] { Integer.TYPE });

                    Integer value9 = new Integer(oldCplModel.getActFrequency());

                    method9.invoke(newModel, value9);

                    return newModel;
                } catch (Exception e) {
                    _log.error(e, e);
                }
            } finally {
                Thread.currentThread().setContextClassLoader(contextClassLoader);
            }
        }

        if (oldModelClassName.equals(hook_categoryClp.class.getName())) {
            hook_categoryClp oldCplModel = (hook_categoryClp) oldModel;

            ClassLoader contextClassLoader = Thread.currentThread()
                                                   .getContextClassLoader();

            try {
                Thread.currentThread().setContextClassLoader(_classLoader);

                try {
                    Class<?> newModelClass = Class.forName("edu.hcmiu.portal.model.impl.hook_categoryImpl",
                            true, _classLoader);

                    Object newModel = newModelClass.newInstance();

                    Method method0 = newModelClass.getMethod("setCatID",
                            new Class[] { Long.TYPE });

                    Long value0 = new Long(oldCplModel.getCatID());

                    method0.invoke(newModel, value0);

                    Method method1 = newModelClass.getMethod("setCatName",
                            new Class[] { String.class });

                    String value1 = oldCplModel.getCatName();

                    method1.invoke(newModel, value1);

                    Method method2 = newModelClass.getMethod("setCatDescription",
                            new Class[] { String.class });

                    String value2 = oldCplModel.getCatDescription();

                    method2.invoke(newModel, value2);

                    return newModel;
                } catch (Exception e) {
                    _log.error(e, e);
                }
            } finally {
                Thread.currentThread().setContextClassLoader(contextClassLoader);
            }
        }

        if (oldModelClassName.equals(hook_taglistClp.class.getName())) {
            hook_taglistClp oldCplModel = (hook_taglistClp) oldModel;

            ClassLoader contextClassLoader = Thread.currentThread()
                                                   .getContextClassLoader();

            try {
                Thread.currentThread().setContextClassLoader(_classLoader);

                try {
                    Class<?> newModelClass = Class.forName("edu.hcmiu.portal.model.impl.hook_taglistImpl",
                            true, _classLoader);

                    Object newModel = newModelClass.newInstance();

                    Method method0 = newModelClass.getMethod("setTaglistID",
                            new Class[] { Long.TYPE });

                    Long value0 = new Long(oldCplModel.getTaglistID());

                    method0.invoke(newModel, value0);

                    Method method1 = newModelClass.getMethod("setTaglistName",
                            new Class[] { String.class });

                    String value1 = oldCplModel.getTaglistName();

                    method1.invoke(newModel, value1);

                    return newModel;
                } catch (Exception e) {
                    _log.error(e, e);
                }
            } finally {
                Thread.currentThread().setContextClassLoader(contextClassLoader);
            }
        }

        if (oldModelClassName.equals(hook_userClp.class.getName())) {
            hook_userClp oldCplModel = (hook_userClp) oldModel;

            ClassLoader contextClassLoader = Thread.currentThread()
                                                   .getContextClassLoader();

            try {
                Thread.currentThread().setContextClassLoader(_classLoader);

                try {
                    Class<?> newModelClass = Class.forName("edu.hcmiu.portal.model.impl.hook_userImpl",
                            true, _classLoader);

                    Object newModel = newModelClass.newInstance();

                    Method method0 = newModelClass.getMethod("setUserID",
                            new Class[] { Long.TYPE });

                    Long value0 = new Long(oldCplModel.getUserID());

                    method0.invoke(newModel, value0);

                    Method method1 = newModelClass.getMethod("setUserName",
                            new Class[] { String.class });

                    String value1 = oldCplModel.getUserName();

                    method1.invoke(newModel, value1);

                    Method method2 = newModelClass.getMethod("setUserEmail",
                            new Class[] { String.class });

                    String value2 = oldCplModel.getUserEmail();

                    method2.invoke(newModel, value2);

                    return newModel;
                } catch (Exception e) {
                    _log.error(e, e);
                }
            } finally {
                Thread.currentThread().setContextClassLoader(contextClassLoader);
            }
        }

        return oldModel;
    }

    public static Object translateInput(List<Object> oldList) {
        List<Object> newList = new ArrayList<Object>(oldList.size());

        for (int i = 0; i < oldList.size(); i++) {
            Object curObj = oldList.get(i);

            newList.add(translateInput(curObj));
        }

        return newList;
    }

    public static Object translateInput(Object obj) {
        if (obj instanceof BaseModel) {
            return translateInput((BaseModel) obj);
        } else if (obj instanceof List) {
            return translateInput((List<Object>) obj);
        } else {
            return obj;
        }
    }

    public static Object translateOutput(BaseModel oldModel) {
        Class<?> oldModelClass = oldModel.getClass();

        String oldModelClassName = oldModelClass.getName();

        if (oldModelClassName.equals(
                    "edu.hcmiu.portal.model.impl.hook_activityImpl")) {
            ClassLoader contextClassLoader = Thread.currentThread()
                                                   .getContextClassLoader();

            try {
                Thread.currentThread().setContextClassLoader(_classLoader);

                try {
                    hook_activityClp newModel = new hook_activityClp();

                    Method method0 = oldModelClass.getMethod("getActID");

                    Long value0 = (Long) method0.invoke(oldModel,
                            (Object[]) null);

                    newModel.setActID(value0.longValue());

                    Method method1 = oldModelClass.getMethod("getActName");

                    String value1 = (String) method1.invoke(oldModel,
                            (Object[]) null);

                    newModel.setActName(value1);

                    Method method2 = oldModelClass.getMethod("getUserID");

                    Long value2 = (Long) method2.invoke(oldModel,
                            (Object[]) null);

                    newModel.setUserID(value2.longValue());

                    Method method3 = oldModelClass.getMethod("getCatID");

                    Long value3 = (Long) method3.invoke(oldModel,
                            (Object[]) null);

                    newModel.setCatID(value3.longValue());

                    Method method4 = oldModelClass.getMethod("getTaglistID");

                    Long value4 = (Long) method4.invoke(oldModel,
                            (Object[]) null);

                    newModel.setTaglistID(value4.longValue());

                    Method method5 = oldModelClass.getMethod("getActTime");

                    Date value5 = (Date) method5.invoke(oldModel,
                            (Object[]) null);

                    newModel.setActTime(value5);

                    Method method6 = oldModelClass.getMethod("getActDuration");

                    Integer value6 = (Integer) method6.invoke(oldModel,
                            (Object[]) null);

                    newModel.setActDuration(value6.intValue());

                    Method method7 = oldModelClass.getMethod("getActSubject");

                    String value7 = (String) method7.invoke(oldModel,
                            (Object[]) null);

                    newModel.setActSubject(value7);

                    Method method8 = oldModelClass.getMethod("getActModel");

                    String value8 = (String) method8.invoke(oldModel,
                            (Object[]) null);

                    newModel.setActModel(value8);

                    Method method9 = oldModelClass.getMethod("getActFrequency");

                    Integer value9 = (Integer) method9.invoke(oldModel,
                            (Object[]) null);

                    newModel.setActFrequency(value9.intValue());

                    return newModel;
                } catch (Exception e) {
                    _log.error(e, e);
                }
            } finally {
                Thread.currentThread().setContextClassLoader(contextClassLoader);
            }
        }

        if (oldModelClassName.equals(
                    "edu.hcmiu.portal.model.impl.hook_categoryImpl")) {
            ClassLoader contextClassLoader = Thread.currentThread()
                                                   .getContextClassLoader();

            try {
                Thread.currentThread().setContextClassLoader(_classLoader);

                try {
                    hook_categoryClp newModel = new hook_categoryClp();

                    Method method0 = oldModelClass.getMethod("getCatID");

                    Long value0 = (Long) method0.invoke(oldModel,
                            (Object[]) null);

                    newModel.setCatID(value0.longValue());

                    Method method1 = oldModelClass.getMethod("getCatName");

                    String value1 = (String) method1.invoke(oldModel,
                            (Object[]) null);

                    newModel.setCatName(value1);

                    Method method2 = oldModelClass.getMethod(
                            "getCatDescription");

                    String value2 = (String) method2.invoke(oldModel,
                            (Object[]) null);

                    newModel.setCatDescription(value2);

                    return newModel;
                } catch (Exception e) {
                    _log.error(e, e);
                }
            } finally {
                Thread.currentThread().setContextClassLoader(contextClassLoader);
            }
        }

        if (oldModelClassName.equals(
                    "edu.hcmiu.portal.model.impl.hook_taglistImpl")) {
            ClassLoader contextClassLoader = Thread.currentThread()
                                                   .getContextClassLoader();

            try {
                Thread.currentThread().setContextClassLoader(_classLoader);

                try {
                    hook_taglistClp newModel = new hook_taglistClp();

                    Method method0 = oldModelClass.getMethod("getTaglistID");

                    Long value0 = (Long) method0.invoke(oldModel,
                            (Object[]) null);

                    newModel.setTaglistID(value0.longValue());

                    Method method1 = oldModelClass.getMethod("getTaglistName");

                    String value1 = (String) method1.invoke(oldModel,
                            (Object[]) null);

                    newModel.setTaglistName(value1);

                    return newModel;
                } catch (Exception e) {
                    _log.error(e, e);
                }
            } finally {
                Thread.currentThread().setContextClassLoader(contextClassLoader);
            }
        }

        if (oldModelClassName.equals(
                    "edu.hcmiu.portal.model.impl.hook_userImpl")) {
            ClassLoader contextClassLoader = Thread.currentThread()
                                                   .getContextClassLoader();

            try {
                Thread.currentThread().setContextClassLoader(_classLoader);

                try {
                    hook_userClp newModel = new hook_userClp();

                    Method method0 = oldModelClass.getMethod("getUserID");

                    Long value0 = (Long) method0.invoke(oldModel,
                            (Object[]) null);

                    newModel.setUserID(value0.longValue());

                    Method method1 = oldModelClass.getMethod("getUserName");

                    String value1 = (String) method1.invoke(oldModel,
                            (Object[]) null);

                    newModel.setUserName(value1);

                    Method method2 = oldModelClass.getMethod("getUserEmail");

                    String value2 = (String) method2.invoke(oldModel,
                            (Object[]) null);

                    newModel.setUserEmail(value2);

                    return newModel;
                } catch (Exception e) {
                    _log.error(e, e);
                }
            } finally {
                Thread.currentThread().setContextClassLoader(contextClassLoader);
            }
        }

        return oldModel;
    }

    public static Object translateOutput(List<Object> oldList) {
        List<Object> newList = new ArrayList<Object>(oldList.size());

        for (int i = 0; i < oldList.size(); i++) {
            Object curObj = oldList.get(i);

            newList.add(translateOutput(curObj));
        }

        return newList;
    }

    public static Object translateOutput(Object obj) {
        if (obj instanceof BaseModel) {
            return translateOutput((BaseModel) obj);
        } else if (obj instanceof List) {
            return translateOutput((List<Object>) obj);
        } else {
            return obj;
        }
    }
}
